# 使用requests库访问百度
# 导包
import requests

# 发送请求
# response = requests.get(url="https://www.baidu.com")
# 查看响应状态码
# print(response.status_code)
# 设置编码格式为UTF-8
# response.encoding = "UTF-8"
# 查看文本格式的响应数据
# print(response.text)

# 访问百度搜索武汉天气的接口,并查看响应数据
# response1 = requests.get(url='http://www.baidu.com/s?wd=武汉天气')
# url = 'http://www.baidu.com/s'
# test_data = {'wd': '武汉天气'}
# response2 = requests.get(url=url, params=test_data)

# 查看响应状态码和响应问文本数据
# print(response1.status_code)
# print(response2.status_code)
# print(response1.text)
# print(response2.text)
# 请求行url
# url1 = "http://127.0.0.1:8000/api/add_event/"
# url2 = "http://47.108.206.100:8080/admin/login"
# 请求头参数,可以自动添加
# headers1 = {"Content-Type": "application/x-www-form-urlencoded"}
# headers2 = {"Content-Type": "application/json"}
# 请求体数据
# test_data1 = {
#     "eid": "12839",
#     "name": "大米发布会",
#     "limit": "100",
#     "status": "1",
#     "address": "光谷小米中心",
#     "start_time": "2022-05-30",
# }
# test_data2 = {
#     "username": "admin",
#     "password": "macro123"
# }
# 发送请求
# response1 = requests.post(url=url1, headers=headers1, data=test_data1)
# response2 = requests.post(url=url2, headers=headers2, data=test_data2)
# 查看响应,查看json返回数据
# print(response1.json())
# print(response2.json())

response = requests.get('http://www.baidu.com/')
print(response.status_code)
print(response.url)
# 获取响应字符编码
print(response.encoding)
# 设置编码格式为UTF-8
response.encoding = 'UTF-8'
print(response.encoding)
# 获取响应头数据
print(response.headers)
print(response.headers.get('Content-Type'))
response1 = requests.get(url="http://www.weather.com.cn/data/sk/101010100.html")
response1.encoding = 'UTF-8'
print(response1.json())
